package com.binvshe.binvshe.mine.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.utils.MD5Util;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/7/26
 */
public class HeadUploadUtils {

    private static final String AppSecret = "zwm23KOH6epXuZh9";
    private static final String AppId = "modo";

    /**
     * Method Name: uploadHead
     */
    public static void uploadHead(final Context context, String filepath, final HeadUploadable impl) {
        File file = new File(filepath);
        IHttpRequest request = new HttpRequest(context);
        String nonce = System.currentTimeMillis() + "";
        String checksum = MD5Util.getMd5(AppSecret + nonce);
        UrlParams urlParams = new UrlParams();
        urlParams.put(ParamKey.FILE, file);
        urlParams.put(ParamKey.APP_ID, AppId);
        urlParams.put(ParamKey.NONCE, nonce);
        urlParams.put(ParamKey.CHECKSUM, checksum);
        request.post(UrlCenter.UPLOAD_USERHEAD, urlParams, new DataRequestListener<String>(String.class) {

            @Override
            public void onSuccess(String s) {
                try {
                    Log.d("kyluzoi", s);
                    JSONObject jobj = new JSONObject(s);
                    String url = jobj.optString("url");
                    impl.onSuccess(url);
                    Log.d("kyluzoi", url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(context, strMsg, Toast.LENGTH_SHORT).show();
                impl.onFailureUpload(t, errorNo, strMsg);
                Log.d("kyluzoi", "抛出一个异常");
            }
        });
    }

    /**
     * 批量上传
     */
    public static void uploadMulti(Context context, final ArrayList<File> files, final MulitUploadable impl) {
        IHttpRequest request = new MyHttpRequest(context);
        String nonce = System.currentTimeMillis() + "";
        String checksum = MD5Util.getMd5(AppSecret + nonce);
        UrlParams params = new UrlParams();
        for (File file : files) {
            params.put(ParamKey.FILE, file);
        }
        params.put(ParamKey.APP_ID, AppId);
        params.put(ParamKey.NONCE, nonce);
        params.put(ParamKey.CHECKSUM, checksum);
        request.post(UrlCenter.UPLOAD_MULIT, params, new DataRequestListener<ArrayList<Urls>>(Urls.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(ArrayList<Urls> o) {
                impl.onSuccess(o);
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                impl.onFailureUpload(t,errorNo,strMsg);
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    public interface HeadUploadable {
        void onSuccess(String url);
        void onFailureUpload(Throwable t, int errorNo, String strMsg);
    }
    public interface MulitUploadable{
        void onSuccess(ArrayList<Urls> urls);
        void onFailureUpload(Throwable t, int errorNo, String strMsg);
    }
}
